package com.demo.common.constant.api.base;

import com.github.pagehelper.PageInfo;
import com.demo.common.result.ResponseInfo;
import com.demo.common.result.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


public class BaseController<T, K> extends AbstractController<T, K> {
    @Autowired
    private BaseServiceImpl<T, K> myService;

    public BaseServiceImpl<T, K> getMyService() {
        return this.myService;
    }

    //测试
    @Override
    @GetMapping("/test")
    public String test() {
        String msg = this.getClass().getSimpleName();
        msg += " is testing";
        System.out.print(msg);
        return msg;
    }

    //查询全部列表
    @Override
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultVo<List<T>> findAll(){
        return ResponseInfo.<List<T>>success(myService.findAll());
    }

    //根据ID查询
    @Override
    @RequestMapping(value = "/search_id", method = RequestMethod.POST)
    public ResultVo<T> findById(@RequestBody IDEntity<K> idt) {
        K id = (K)idt.getId();
        return ResponseInfo.<T>success(myService.findById(id));
    }

    //多条件查询
    @Override
    @RequestMapping(value = "/search_params", method = RequestMethod.POST)
    public ResultVo<List<T>> findByParams(@RequestBody Map<String, String> map) {
        return ResponseInfo.<List<T>>success(myService.findByParams(map));
    }


    //查询记录总数
    @Override
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    public ResultVo<Integer> findAllCount() {
        return ResponseInfo.<Integer>success(myService.findAllCount());
    }

    //分页查询
    @Override
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResultVo findPage(@RequestBody Map<String, String> map){
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        return ResponseInfo.<PageInfo<T>>success(myService.findPage(map, pageNum, pageSize));
    }

    //增加
    @Override
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultVo add(@RequestBody T entity){
        //System.out.println(entity);
        if (myService.add(entity)) {
            return ResponseInfo.success();
        } else {
            return ResponseInfo.fail("添加数据失败！");
        }
    }

    //修改
    @Override
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultVo modify(@RequestBody T entity){
        System.out.println(entity);
        if (myService.modify(entity)) {
            return ResponseInfo.success();
        } else {
            return ResponseInfo.fail("修改数据失败！");
        }
    }

    //多条件修改
    @Override
    @RequestMapping(value = "/update_params", method = RequestMethod.POST)
    public ResultVo modifyByParams(@RequestBody T entity) {
        if (myService.modifyByParams(entity)) {
            return ResponseInfo.success();
        } else {
            return ResponseInfo.fail("修改数据失败！");
        }
    }

    //根据ID删除
    @Override
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultVo remove(@RequestBody IDEntity<K> record) {
        if (myService.remove(record.getId())) {
            return ResponseInfo.success();
        } else {
            return ResponseInfo.fail("删除数据失败！");
        }
    }

    //根据ID批量删除
    @Override
    @RequestMapping(value = "/delete_batch", method = RequestMethod.POST)
    public ResultVo batchRemove(@RequestBody List<K> ids){
        if (myService.batchRemove(ids)) {
            return ResponseInfo.success();
        } else {
            return ResponseInfo.fail("删除数据失败！");
        }
    }

    //多条件删除
    @Override
    @RequestMapping(value = "/delete_params", method = RequestMethod.POST)
    public ResultVo removeByParams(@RequestParam Map<String, String> map){
        if (myService.removeByParams(map)) {
            return ResponseInfo.success();
        } else {
            return ResponseInfo.fail("删除数据失败！");
        }
    }
}
